// 资产库可用资产信息
var purchaseProductDatas = new Array();

$$scope.submmitData = {};
$$scope.submmitData.purchaseStepEntity = {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId(),
}
$$scope.select = {};
// 采购记录
var proc1Columns = [{
    id: 'commodityCateName',
    title: '类目',
    width: 73,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'commodityClassName',
    title: '类别',
    width: 73,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'name',
    title: '名称',
    width: 73,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'descAndSpec',
    title: '品名及规格',
    width: 200,
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'quantity',
    title: '数量',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'estimatedAmount',
    title: '预计金额',
    width: 150,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '￥' + value + '元';
        return content;
      }
}, {
    id: 'remark',
    title: '备注',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'operation',
    title: '操作',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        //data-toggle="modal" href="#myModal"
        if (record.bigClassCode == "SB") {
            content += '<button class="btn btn-xs btn-info" onclick="openModal(\'' + record.commodityCateCode + '\',\'' + record.commodityClassCode + '\',\'' + record.biCode + '\')" ><i class="icon-retweet"></i>  调配资产</button>';
        }
        return content;
    }
}];
var productSelectDatagrid = null;
var nowBiCode = '';

function openModal(commodityCateCode, commodityClassCode, biCodes) {

    if (productSelectDatagrid == null) {
        productSelectDatagrid = $CertusJsonAjax.dataInit("findAssetsList", null, lidColumns, 'lidContainer', 'lidToolBarContainer',5, [10]);
    }

    nowBiCode = biCodes
    $$scope.select.assetBigClass = commodityCateCode;
    $$scope.select.productType = commodityClassCode;
    productSelectDatagrid.parameters = {
        code: 9,
        param: commodityClassCode
    };
    productSelectDatagrid.refresh(true);
    $$scope.$apply();
    $("#myModal").modal('show');
}
// 资产库借货记录
var library1Columns = [
    //     {
    //     id: 'number',
    //     title: '序号',
    //     width: 45,
    //     type: 'string',
    //     columnClass: 'text-center',
    // }, 
    {
        id: 'asset_style1',
        title: '资产类目',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'asset_style2',
        title: '资产类别',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'asset_id',
        title: '资产编号',
        width: 170,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'comments',
        title: '资产描述',
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'asset_status',
        title: '状态',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'createUserId',
        title: '归属人',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'pro_name',
        title: '归属项目',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'city_location',
        title: '放置地',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'operation',
        title: '操作',
        width: 180,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            if(record.assetLocked == 'locked'){
                content += '<button class="btn btn-xs btn-success" onclick="unding(\'' + record.asset_id + '\',this);"><i class="icon-ok"></i>  取消预订</button>';
                content += '  ';
            }else{
                content += '<button class="btn btn-xs btn-info" onclick="zdbox1(\'' + record.asset_id + '\');"><i class="icon-retweet"></i>  转换采购</button>';
                content += '  ';
                content += '<button class="btn btn-xs btn-success" onclick="ding(\'' + record.asset_id + '\',this);"><i class="icon-ok"></i>  申请预定</button>';
            }
            return content;
        }
    }
];

function ding(asset_id, b) {
    layer.confirm('是否确认申请预定资产？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        layer.msg('预定成功', {
            icon: 1
        });
        $CertusFormAjax.ajax("assetLock", {
            asset_id: asset_id,
            projId: $$scope.headerMap.projId,

        }, function (res) {
            var exhibitDatas = belowGrid.exhibitDatas;
            for(var i in exhibitDatas){
                if (exhibitDatas[i].asset_id == asset_id) {
                    exhibitDatas[i].assetLocked = 'locked';
                }
            }
            belowGrid.exhibitDatas = exhibitDatas;
            belowGrid.refresh(false);
        });
    });
}

function unding(asset_id, b) {
    layer.confirm('是否确认取消预定资产？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        layer.msg('预定成功', {
            icon: 1
        });
        $CertusFormAjax.ajax("assetunLock", {
            asset_id: asset_id,
            projId: $$scope.headerMap.projId
        }, function (res) {
            var exhibitDatas = belowGrid.exhibitDatas;
            for(var i in exhibitDatas){
                if (exhibitDatas[i].asset_id == asset_id) {
                    exhibitDatas[i].assetLocked = 'unlocked';
                }
            }
            belowGrid.exhibitDatas = exhibitDatas;
            belowGrid.refresh(false);
        });
    });
}



function isNotSameAsset(element) {
    return element.asset_id == bigAssetCode;
}

function zdbox1(assetCode) {

    //去除
    var exhibitDatas = belowGrid.exhibitDatas;
    bigAssetCode = assetCode;
    var removeItem = exhibitDatas.filter(isNotSameAsset);
    exhibitDatas = exhibitDatas.filter(isSameAsset);
    belowGrid.exhibitDatas = exhibitDatas;
    belowGrid.refresh(false);
    //添加
    var exhibitDatas2 = aboveGrid.exhibitDatas;
    ;
    var addAbove = {
        "commodityCateName": removeItem[0].asset_style1,
        "commodityClassName": removeItem[0].asset_style2,
        "name": removeItem[0].asset_name,
        "quantity": 1,
        "descAndSpec": removeItem[0].comments,
        "estimatedAmount": 0,
        "bigClassCode": "SB",
        "commodityCateCode":removeItem[0].assetCode ,
        "commodityClassCode": removeItem[0].style_id,
        "remark": "来自 转换采购",
        "brCode": brCode,
        "mark": "second",
        "biCode": "BI-" + new Date().getTime() + Math.floor(Math.random() * 10000)
    }
    exhibitDatas2.push(addAbove);
    aboveGrid.exhibitDatas = exhibitDatas2;
    aboveGrid.refresh(false);
}

var lidColumns = [{
    id: 'asset_id',
    title: '资产编号',
    width: 170,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'city_location',
    title: '放置地',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'comments',
    title: '资产描述',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'operation',
    title: '操作',
    width: 60,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        for (var i in belowGrid.exhibitDatas) {
            if (belowGrid.exhibitDatas[i].asset_id == record.asset_id) {
                content += '<button class="btn btn-xs btn-info" onclick="applyProductTwo(\'' + record.asset_id + '\',this);" disabled>已申请</button>';
                return content;
            }
        }
        content += '<button class="btn btn-xs btn-info" onclick="applyProductTwo(\'' + record.asset_id + '\',this);"><i class="icon-ok"></i>  申请</button>';
        return content;
    }
}];

var belowGrid;
var aboveGrid;
var brCode;
$CertusJsonAjax.ajax("purchaseBaseInfo", {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
}, function (res) {
    if (res.data) {

        $$scope.headerMap = res.data.headerMap;
        $$scope.processs = res.data.processs;
        brCode = res.data.headerMap.brCode;
        $$scope.submmitData.purchaseStepEntity.buyingRequisitionCode = res.data.headerMap.brCode;
        $$scope.submmitData.purchaseStepEntity.projId = res.data.headerMap.projId;
        var priceOption1 = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '采购单条目信息',
            datas: res.data.buyings,
            columns: proc1Columns,
            gridContainer: 'proc3Container',
        };
        aboveGrid = $.fn.dlshouwen.grid.init(priceOption1);
        aboveGrid.load();

        var priceOption2 = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '资产库申请清单',
            datas: res.data.inventors,
            columns: library1Columns,
            gridContainer: 'library3Container'
        };
        belowGrid = $.fn.dlshouwen.grid.init(priceOption2);
        belowGrid.load();

    }
})

$$scope.searchProduct = function () {
    productSelectDatagrid.parameters = {
        code: 9,
        param: $$scope.select.productType
    };
    productSelectDatagrid.refresh(true);
}


$CertusFormAjax.ajax("findStyles", {}, function (response) {
    $$scope.select.assetClassSelect = response.data;
});

$CertusFormAjax.ajax("assetBigClass", {}, function (response) {
    $$scope.select.assetBigClassSelect = response.data;
});

var bigAssetCode = '';

function isSameAsset(element) {
    return element.asset_id != bigAssetCode;
}

$$scope.getsssbbb = function(bb){
    ;
    //starting denied sentback others completed
    if(bb == "starting"){
        return "正在进行中";
    }else if(bb == "denied"){
        return "否决";
    }else if(bb == "sentback"){
        return "被退";
    }else if(bb == "others"){
        return "关闭";
    }else if(bb == "completed"){
         return "完成";    }else if(bb == "terminated"){        return "终结";    }    else if(bb == "cancelled"){        return "通过";}
    else if(bb == "cancelled"){
        return "通过";    }
    return "待办";
}

function isSameItem(element) {
    return element.biCode != nowBiCode
}

function applyProductTwo(assetCode, b) {

    var exhibitionData = belowGrid.exhibitDatas;
    bigAssetCode = assetCode;

    exhibitionData = exhibitionData.filter(isSameAsset);
    for (var i in productSelectDatagrid.exhibitDatas) {
        if (productSelectDatagrid.exhibitDatas[i].asset_id == assetCode) {
            exhibitionData.push(productSelectDatagrid.exhibitDatas[i]);
        }
    }
    $(b).html(" 已申请")
    belowGrid.exhibitDatas = exhibitionData;
    belowGrid.refresh(false);

    //减去 原有 数据
    var exhibitDatas = aboveGrid.exhibitDatas;
    for (var i in exhibitDatas) {
        if (exhibitDatas[i].biCode == nowBiCode) {

            var number = parseInt(exhibitDatas[i].quantity);
            if (number > 1) {
                exhibitDatas[i].quantity = number - 1;
                break;
            } else {
                exhibitDatas = exhibitDatas.filter(isSameItem);
                $("#myModal").modal('hide');
                break;
            }
        }
    }
    aboveGrid.exhibitDatas = exhibitDatas;
    aboveGrid.refresh(false);
}

$$scope.approvalApply = function () {
    if (certusValidateNotnull()) {
        $$scope.submmitData.productApplyItems = belowGrid.exhibitDatas;
        $$scope.submmitData.purchaseItems = aboveGrid.exhibitDatas;
        $$scope.submmitData.findWho = "A";
        $CertusJsonAjax.ajax("assetManager", $$scope.submmitData, function (res) {
            if (res.data) {
                if ("success" == res.data) {
                    findBacklogPerson($CertusFormAjax, $CertusJsonAjax.getStepInstanceId());
                    window.history.back();
                    return;
                }
                layer.msg(res.data);
            }
        })
    }
}